package com.wanhelibs.custom_view.views;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.wanhe.base.BaseDialogFragment;
import com.wanhe.base.model.base.Dish;
import com.wanhe.shoppingcart.bean.SetMealCartItem;
import com.wanhe.shoppingcart.manager.CartManager;
import com.wanhe.shoppingcart.manager.ICartChangeObserver;
import com.wanhe.shoppingcart.manager.ICartItem;
import com.wanhelibs.R;
import com.wanhelibs.adapter.ReduceSetMealAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by XieZaiWei on 2017/8/12.
 * 减套餐Dialog
 */

public class ReduceSetMealDialog extends BaseDialogFragment implements ICartChangeObserver {

	private TextView			tv_title;
	private ExpandableListView	lv_setMeal;

	private Dish					mDish;
	private ReduceSetMealAdapter	mReduceSetMealAdapter;

	public static ReduceSetMealDialog newInstance() {
		ReduceSetMealDialog dialog = new ReduceSetMealDialog();
		return dialog;
	}

	public void setDish(Dish dish) {
		mDish = dish;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		mView = inflater.inflate(R.layout.reduce_set_meal_dialog, container, false);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		tv_title = (TextView) findViewById(R.id.tv_title);
		lv_setMeal = (ExpandableListView) findViewById(R.id.lv_setMeal);

		tv_title.setText(mDish.getVch_dishname());
		lv_setMeal.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition,
					long id) {
				return true;
			}
		});

		mReduceSetMealAdapter = new ReduceSetMealAdapter(getActivity(),
				R.layout.reduce_set_meal_item, R.layout.reduce_set_meal_child_item);
		lv_setMeal.setAdapter(mReduceSetMealAdapter);

		CartManager.getInstance().addCartObserver(this);
		showSetMeal();
	}

	@Override
	public void onStart() {
		super.onStart();
		Dialog dialog = getDialog();
		if (dialog != null) {
			DisplayMetrics dm = new DisplayMetrics();
			getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
			dialog.getWindow().setLayout((int) (dm.widthPixels * 0.4), (int) (dm.widthPixels * 0.5));
		}
	}

	private void showSetMeal() {
		Map <String,
				ICartItem> setMealMap = CartManager.getInstance().getItems(mDish.getDish_guid());
		if(setMealMap != null){
			Collection <ICartItem> valueCollection = setMealMap.values();
			ArrayList <SetMealCartItem> arrSetMeal = new ArrayList<>();
			Iterator <ICartItem> iterator = valueCollection.iterator();
			while (iterator.hasNext()) {
				SetMealCartItem setMealCartItem = (SetMealCartItem) iterator.next();
				arrSetMeal.add(setMealCartItem);
			}
			mReduceSetMealAdapter.setData(arrSetMeal);
			for (int i = 0; i < mReduceSetMealAdapter.getGroupCount(); i++) {
				lv_setMeal.expandGroup(i);
			}
		}else {
			mReduceSetMealAdapter.clean();
		}

	}

	@Override
	public boolean isNeedUpdate() {
		return true;
	}

	@Override
	public void onCartChanged(boolean isAdd) {
		// 只监听减的事件
		if (!isAdd) {
			showSetMeal();
		}

	}
}
